package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.constant.TaskConstant;
import com.example.demo.controller.bean.Result;
import com.example.demo.mapper.BadgeManageMapper;
import com.example.demo.mapper.entity.*;
import com.example.demo.service.BadgeManageService;
import com.example.demo.utils.PageBo;
import com.example.demo.utils.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BadgeManageServiceImpl extends ServiceImpl<BadgeManageMapper, Badge> implements BadgeManageService {
    @Autowired
    private BadgeManageMapper mapper;

    @Override
    public List<BadgeView> getTabsList() {
        List<BadgeView> logs = mapper.getBadgeTabs();
        return logs;
    }

    @Override
    public PageVo<BadgeView> getBadgeList(BadgeView badge) {
        PageBo pageBo = new PageBo();
        pageBo.setPageNum(badge.getPageNum());
        pageBo.setPageSize(badge.getPageSize());

        List<BadgeView> logs = mapper.getBadgeList(badge);

        return new PageVo<>(pageBo, logs);
    }

    @Override
    public PageVo<BadgeView> getGrowthBadgeList(PageBo pageBo) {
        List<BadgeView> logs = mapper.getGrowthBadgeList();
        for (BadgeView badge : logs) {
            String requirement = TaskConstant.badgeGrowthRequirement
                    .replace("attribute_name", badge.getAttributeName())
                    .replace("attribute_value",Integer.toString(badge.getAttributeValue()));
            badge.setRequirement(requirement);
        }
        return new PageVo<>(pageBo, logs);
    }

    @Override
    public PageVo<BadgeView> getAchievementBadgeList(PageBo pageBo) {
        List<BadgeView> logs = mapper.getAchievementBadgeList();
        for (BadgeView badge : logs) {
            String requirement = TaskConstant.badgeAchievementRequirement
                    .replace("task_name", badge.getTaskName());
            badge.setRequirement(requirement);
        }
        return new PageVo<>(pageBo, logs);
    }

    @Override
    public PageVo<BadgeView> getSpecialBadgeList(PageBo pageBo) {
        List<BadgeView> logs = mapper.getSpecialBadgeList();
        for (BadgeView badge : logs) {
            String requirement = TaskConstant.badgeSpecialRequirement
                    .replace("badge_number", Integer.toString(badge.getBadgeNumber()));
            badge.setRequirement(requirement);
        }
        return new PageVo<>(pageBo, logs);
    }

    @Override
    public PageVo<BadgeView> searchBadgeList(BadgeView request,PageBo pageBo) {
        BadgeView newBadge = new BadgeView();
        newBadge.setType(request.getType());
        newBadge.setBadgeName(request.getBadgeName());
        newBadge.setAttributeName(request.getAttributeName());
        newBadge.setTaskType(request.getTaskType());

        System.out.println("徽章信息"+newBadge);
        List<BadgeView> logs = new ArrayList<>();
        if(request.getType()==1){
            logs = mapper.getSelectBadge(newBadge);
            for (BadgeView badge : logs) {
                String requirement = TaskConstant.badgeGrowthRequirement
                        .replace("attribute_name", badge.getAttributeName())
                        .replace("attribute_value",Integer.toString(badge.getAttributeValue()));
                badge.setRequirement(requirement);
            }
        } else if (request.getType()==2) {
            logs = mapper.getSelectBadge(newBadge);
            for (BadgeView badge : logs) {
                String requirement = TaskConstant.badgeAchievementRequirement
                        .replace("task_name", badge.getTaskName());
                badge.setRequirement(requirement);
            }
        } else if (request.getType()==3) {
            logs = mapper.getSelectBadge(newBadge);
            for (BadgeView badge : logs) {
                String requirement = TaskConstant.badgeSpecialRequirement
                        .replace("badge_number", Integer.toString(badge.getBadgeNumber()));
                badge.setRequirement(requirement);
            }
            return new PageVo<>(pageBo, logs);
        }

        return new PageVo<>(pageBo, logs);
    }

    @Override
    public Result<String> addBadge(BadgeView request) {
        Badge newBadge = new Badge();
        newBadge.setName(request.getBadgeName());
        newBadge.setDescription(request.getDescription());
        newBadge.setType(request.getType());
        newBadge.setTaskId(request.getTaskId());
        newBadge.setAttributeId(request.getAttributeId());
        newBadge.setAttributeValue(request.getAttributeValue());
        newBadge.setBadgeNumber(request.getBadgeNumber());

        if(newBadge!=null){
            if (mapper.insert(newBadge) > 0) {
                return Result.ok("添加成功");
            } else {
                return Result.fail("添加失败");
            }
        }
        return Result.fail("添加失败,接收值爲空");
    }

    @Override
    public Result<String> deleteBadge(BadgeView request) {
        int id=request.getBadgeId();
        if(id!=0){
            mapper.deleteBadgeById(request);
            return Result.ok("删除成功");
        }else{
            return Result.error(400, "删除失败");
        }
    }

    @Override
    public void batchDeleteBadge(List<Badge> badgeList) {
        mapper.batchDeleteBadge(badgeList);
    }

    @Override
    public Result<String> updateBadgeList(BadgeView request) {
        Badge newBadge = new Badge();
        newBadge.setBadgeId(request.getBadgeId());
        newBadge.setName(request.getBadgeName());
        newBadge.setDescription(request.getDescription());
        newBadge.setType(request.getType());
        newBadge.setAttributeId(request.getAttributeId());
        newBadge.setAttributeValue(request.getAttributeValue());
        newBadge.setBadgeNumber(request.getBadgeNumber());
        newBadge.setRequirement(request.getRequirement());

        if(newBadge!=null){
            mapper.updateBadgeList(newBadge);
            return Result.ok("修改成功");
        }
        else {
            return Result.fail("修改失败");
        }
    }

    @Override
    public Badge getById(int badgeId) {
        return mapper.selectImageUrl(badgeId);
    }

    @Override
    public Badge selectBadgeById(int badgeId) {
        return mapper.selectBadgeById(badgeId);
    }

    @Override
    public void placeBadgePic(Badge currentBadge) {
        mapper.placeBadgePic(currentBadge);
    }

    @Override
    public List<BadgeView> list(LambdaQueryWrapper<BadgeView> queryWrapper) {
        return null;
    }

    @Override
    public PageVo<StudentBadgeView> getBadgeLog(PageBo pageBo) {
        List<StudentBadgeView> logs = mapper.getBadgeLog();
        return new PageVo<>(pageBo, logs);
    }

    @Override
    public PageVo<StudentBadgeView> searchBadgeLog(StudentBadgeView request, PageBo pageBo) {
        String badgeName=request.getBadgeName();
        int type=request.getType();
        List<StudentBadgeView> list = mapper.getBadgeLog();
        list = list.stream()
                .filter(o ->(badgeName == null || o.getBadgeName().contains(badgeName)) &&
                        (type == 0 || o.getType()==type))
                .collect(Collectors.toList());
        return new PageVo<>(pageBo, list);
    }


}
